//
//  WebPushParameterModel.m
//  ExampleApp-iOS
//
//  Created by QA on 16/6/24.
//  Copyright © 2016年 Marcus Westin. All rights reserved.
//

#import "WebPushParameterModel.h"
#import "NSDictionary+DataFormat.h"

@implementation WebPushParameterModel
@synthesize CodeType;
@synthesize URL;
@synthesize Title;
@synthesize SubTitle;

//解析JSON数据，并生成WebPushParameterModel类数据
- (void)analysisJsonParameterString:(NSString*)jsonString
{
    if (jsonString == nil) return;
    NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    NSError *error;
    
    NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
    if(error) return;
    
    self.CodeType = [dictionary objectFormatIntegerForKey:@"CodeType"];
    self.URL = [dictionary objectFormatStringForKey:@"Url"];
    self.Title = [dictionary objectFormatStringForKey:@"Title"];
    self.SubTitle = [dictionary objectFormatStringForKey:@"SubTitle"];
}

//解析JSON数据，并生成WebPushParameterModel类数据
+ (WebPushParameterModel*)analysisJsonParameter:(id)parameter
{
    if (parameter == nil) return nil;
    NSDictionary *dictionary = nil;
    
    //判断数据类型（容错处理）
    if([parameter isKindOfClass:[NSString class]]){
        NSData *jsonData = [parameter dataUsingEncoding:NSUTF8StringEncoding];
        NSError *error = nil;
        dictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
        if(error) return nil;
        
    } else if([parameter isKindOfClass:[NSDictionary class]]){
        dictionary = parameter;
        
    } else {
        return nil;
    }
    
    //数据打包
    WebPushParameterModel *model = [[WebPushParameterModel alloc] init];
    model.CodeType = [dictionary objectFormatIntegerForKey:@"CodeType"];
    model.URL = [dictionary objectFormatStringForKey:@"Url"];
    model.Title = [dictionary objectFormatStringForKey:@"Title"];
    model.SubTitle = [dictionary objectFormatStringForKey:@"SubTitle"];
    
    return model;
}

@end
